package com.ran.servlets;

import com.ran.service.BookService;
import com.ran.service.IBookServcie;
import com.ran.po.Book;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import com.google.gson.Gson;
@WebServlet("/get_book")
public class GetBook extends HttpServlet {


    private IBookServcie bookServcie;
    @Override
    public void init() throws ServletException {
        bookServcie = new BookService();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setHeader("Access-Control-Allow-Origin", "*");

        // 如果需要支持特定的 HTTP 方法（如 POST, PUT, DELETE），可以添加以下头
        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
        String number = request.getParameter("number");
        response.setContentType("application/json;charset=UTF-8");

        if(number == null ||number.trim().isEmpty()){
            response.getWriter().write("{\"status\":\"error\"}");

        }else{
            Book book = bookServcie.getById(number);
            Gson gson = new Gson();
            String json = gson.toJson(book);
            response.getWriter().write(json);
        }


    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");

    }




}